<template>
  <div class="user">
    <PageSearch
      :searchFormConfig="searchFormConfig"
      @resetBtnClick="handleRestClick"
      @queryClick="handleQueryClick"
    />
    <div class="content">
      <PageContent
        ref="pageContentRef"
        :contentTableConfig="contentTableConfig"
        pageName="users"
        @newBtnClick="handleNewData"
        @editClick="handleEditData"
      />
    </div>
    <PageModal
      ref="pageModalRef"
      pageName="users"
      :defaultInfo="defaultInfo"
      :modalConfig="modalConfigRef"
    ></PageModal>
  </div>
</template>

<script lang="ts">
import PageSearch from '@/components/page-search'
import PageContent from '@/components/page-content'
import PageModal from '@/components/page-modal'

import { modalConfig } from './modal.config'

import { computed, defineComponent } from 'vue'

import { searchFormConfig } from './search.config'
import { contentTableConfig } from './content.config'
import { usePageSearch } from '@/hooks/use-page-search'
import { usePageModal } from '@/hooks/use-page-modal'
import { useStore } from '@/store'

export default defineComponent({
  name: 'user',
  components: { PageSearch, PageContent, PageModal },
  setup() {
    const [pageContentRef, handleRestClick, handleQueryClick] = usePageSearch()
    //新建回调
    const newCallback = () => {
      const passwordItem = modalConfig.formItems.find(
        (item) => item.field === 'password'
      )
      passwordItem!.isHidden = false
    }
    //编辑回调
    const editCallback = () => {
      const passwordItem = modalConfig.formItems.find(
        (item) => item.field === 'password'
      )
      passwordItem!.isHidden = false
    }
    //动态添加部门和角色列表
    const store = useStore()
    const departmentItem = modalConfig.formItems.find(
      (item) => item.field === 'departmentId'
    )
    const roleItem = modalConfig.formItems.find(
      (item) => item.field === 'roleId'
    )
    const modalConfigRef = computed(() => {
      departmentItem!.options = store.state.entireDepartment.map((item) => {
        return { title: item.name, value: item.id }
      })
      roleItem!.options = store.state.entireRole.map((item) => {
        return { title: item.name, value: item.id }
      })

      return modalConfig
    })
    //引入hooks
    const [pageModalRef, defaultInfo, handleEditData, handleNewData] =
      usePageModal(newCallback, editCallback)

    return {
      searchFormConfig,
      contentTableConfig,
      pageContentRef,
      handleRestClick,
      handleQueryClick,
      modalConfigRef,
      handleEditData,
      handleNewData,
      pageModalRef,
      defaultInfo
    }
  }
})
</script>

<style scoped>
.user {
  background: white;
}
</style>
